package com.ithe.user.controller.admin;

import com.ithe.core.result.Result;
import com.ithe.core.utils.WebUtils;
import com.ithe.user.pojo.dto.RegisterDto;
import com.ithe.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @BelongsProject: sso
 * @BelongsPackage: com.gxa.controller
 * @Author: jingjianghao
 * @CreateTime: 2022-08-12  17:03
 * @Description: TODO
 * @Version: 1.0
 */
@RequestMapping("/admin-api/v1/user")
@Controller
@ResponseBody
@Api(value = "用户相关接口文档")
@AllArgsConstructor
@Slf4j
public class UserController {

    private UserService userService;


    @ApiOperation(value = "用户注册接口",httpMethod = "POST")
    @PostMapping("/register")
    public Result register(@RequestBody @Validated RegisterDto registerDto, BindingResult bindingResult){

        //1.校验
        if (bindingResult.hasErrors()){
            Result result =  WebUtils.getErrors(bindingResult);
            log.error("校验失败：{}",result);
            return result;
        }
        //2.校验两次密码输入是否一致
        if (!registerDto.getPassword().equals(registerDto.getPassword2())){
            log.error("两次密码输入不一致");
            return Result.failed(ResultCode.TWO_PASSWORD_NO_EQUALS);
        }
        //3.注册
      userService.register(registerDto);
        return Result.success();
    }




}
